#include "CGymClub.h"
#include "CDevice.h"
#include <string.h>

CGymClub::CGymClub(const  CGymClub &other)
{
strcpy(ClubName,other.ClubName);
for(int i=0;i<other.getTrainerCount();i++)
	{
		Trainers[i] = new CTrainer(*other.Trainers[i]);
		TrainersCount++;
	}


Devices = new CDevice*[other.getDeviceCount()];
for(int i=0;i<other.getDeviceCount();i++)
	{
		Devices[i] = new CDevice(*other.Devices[i]);	
		DevicesCount++;
	}


	
}

CGymClub::CGymClub(const char *name)
{
	if (name!=NULL)
		strcpy_s(ClubName,name);
	TrainersCount = 0;
	DevicesCount = 0;
	for(int i=0;i<MAXTRAINERS;i++)
		Trainers[i] = NULL;
}


CGymClub::~CGymClub(void)
{
	freeAll();
}

resAddTrainer CGymClub::addTrainer(const char *TrainerName)
{
	if (TrainersCount >= MAXTRAINERS)
		return REGISTER_FAIL_CLUB_FULL;

	CTrainer *NewTrainer = new CTrainer(TrainerName);
	
	Trainers[TrainersCount] = NewTrainer;

	TrainersCount++;

	return REGISTER_OK;
}

void CGymClub::addDevice(const char *DeviceName)
{

	CDevice *NewDevice = new CDevice(DeviceName);
	CDevice **DevicesTemp;
	
	DevicesTemp = new CDevice*[DevicesCount+1];
	for(int i=0;i<DevicesCount;i++)
		DevicesTemp[i] = Devices[i];
	DevicesTemp[DevicesCount] = NewDevice;

	if (DevicesCount > 0)
	delete []Devices;

	Devices = new CDevice*[DevicesCount+1];

	for(int i=0;i<=DevicesCount;i++)
		Devices[i] = DevicesTemp[i];

	delete []DevicesTemp;

	DevicesCount++;

}

CTrainer * CGymClub::getTrainerbyName(const char *TrainerName)
{
	CTrainer * ptr = NULL;
	for(int i=0;i<TrainersCount;i++)
		if (strcmp(TrainerName,Trainers[i]->getTrainerName())==0)
		{
			ptr = Trainers[i];
			break;
		}
	return ptr;

}
CDevice * CGymClub::getDevicebyName(const char *DeviceName)
{
	CDevice * ptr = NULL;
	for(int i=0;i<DevicesCount;i++)
		if (strcmp(DeviceName,Devices[i]->getDeviceName())==0)
		{
			ptr = Devices[i];
			break;
		}
	return ptr;

}

const char * CGymClub::getClubName(){	return ClubName;}

const CDevice * CGymClub::getDevice(const int ind)
{
	if (ind<=getDeviceCount())
		return Devices[ind];
	else
		return NULL;
}

const CTrainer * CGymClub::getTrainer(const int ind)
{
	if (ind<=getTrainerCount())
		return Trainers[ind];
	else
		return NULL;
}
void CGymClub::getGymDevices(char ** &Arr)
{
Arr = new char* [DevicesCount];
for(int i=0;i<DevicesCount;i++)
{
	Arr[i] = new char [strlen(Devices[i]->getDeviceName())+1];
	strcpy(Arr[i],Devices[i]->getDeviceName());
}
}

void CGymClub::getGymTrainers(char ** &Arr)
{
Arr = new char* [TrainersCount];
for(int i=0;i<TrainersCount;i++)
{
	Arr[i] = new char [strlen(Trainers[i]->getTrainerName())+1];
	strcpy(Arr[i],Trainers[i]->getTrainerName());
}
}


int CGymClub::getDeviceCount () const {	return DevicesCount;}
int CGymClub::getTrainerCount() const {	return TrainersCount;}

void CGymClub::freeAll()
{
	for(int i=0;i<getTrainerCount();i++)
		delete Trainers[i];
	
	for(int i=0;i<getDeviceCount();i++)
		delete Devices[i];
	if (getDeviceCount()>0)
	delete []Devices;

}

void CGymClub::freeStrings(char ** &Arr,int len)
{
	for(int i=0;i<len;i++)
		delete []Arr[i];
	delete []Arr;
}
